﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using BusinessObjects;
using MainGameModule.Models;
using MainGameModule.Views;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common;
using YourWorld.Common.Infrastucture.Events;

namespace MainGameModule.ViewModels
{
    public class WallOfFameViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;

        private ObservableCollection<Player> players;

        public ObservableCollection<Player> Players
        {
            get
            {
                if (this.players == null)
                    this.players = new ObservableCollection<Player>();

                return this.players;
            }
            set
            {
                if (this.players == value)
                    return;

                this.players = value;
                this.NotifyPropertyChanged("Players");
            }
        }

        public ICommand CloseWallOfFameCommand
        {
            get;
            private set;
        }

        public WallOfFameViewModel(IRegionManager regionManager, IUnityContainer container)
        {
            this.regionManager = regionManager;
            this.container = container;

            this.InitializePlayers();
            this.CloseWallOfFameCommand = new DelegateCommand<string>( this.CloseWallOfFame );

            EventService.Aggregator.GetEvent<LevelFinished>().Subscribe(this.RefreshPlayers);
        }

        public void RefreshPlayers(int level)
        {
            PlayersRepository.Instance.PlayersLoaded += (players) =>
                {
                    this.InitializePlayers();
                };

            PlayersRepository.Instance.Initialize();
        }

        private void InitializePlayers()
        {
            this.Players = new ObservableCollection<Player>();

            foreach (Player player in PlayersRepository.Instance.Players)
            {
                Player newPlayer = new Player()
                {
                    Id = player.Id,
                    Name = player.Name,
                    Experience = player.Experience,
                    Level = player.Level,
                    Image = player.Image,
                    BadgesCollection = player.BadgesCollection,
                    QuestsCollection = player.QuestsCollection,
                    Connections = player.Connections,
                    GameLevelCollectionWorkaround = player.GameLevelCollectionWorkaround,
                    RationedExperience = player.RationedExperience,
                };

                this.Players.Add(newPlayer);
            }
        }

        private void CloseWallOfFame(string notUsed)
        {
            this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate(this.container.Resolve<WallOfFameView>());
        }
    }
}